from ctypes import *
from .cpmem import Mem
import numpy as np

class FTMem(Mem):
    def __init__(self,fn):
        Mem.__init__(self,fn)
        self.lib.done.restype          = c_uint64
        self.lib.transparentRf.restype = c_uint64
        self.lib.fromRf.restype        = c_uint64
        self.lib.transparent.restype   = c_uint64
        self.lib.toRf.restype          = c_uint64

    def cbyte_done(self,buf,done):
        l = len(buf)//2
        return done(c_void_p(self.h),byref(buf),c_int(l))

    def np_done(self,buf,done):
        l = buf.shape[0]
        cbuf = np.ctypeslib.as_ctypes(buf)
        return done(c_void_p(self.h),byref(cbuf),c_int(l))

    def rf_in(self,buf):
        return self.cbyte_done(buf,self.lib.fromRf)

    def tuner(self,ppm,dftr):
        return self.lib.tuner(c_void_p(self.h),c_double(ppm),c_double(dftr))

